package cn.tecnova.transaction

import java.net.InetAddress

import cn.tecnova.utils.ConfigHandler
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.SearchHit
import org.elasticsearch.transport.client.PreBuiltTransportClient

/**
  * description:es测试
  **/
object ES {

  def main(args: Array[String]): Unit = {

    val settings = Settings.builder().put("cluster.name", "es-cluster").build()

    val client = new PreBuiltTransportClient(settings)
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.100.3"), 9300))
      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.100.6"), 9300))
      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.100.7"), 9300))
//      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.17.22.17"), ConfigHandler.esPort))
//    val client = ESUtils.getEsClient()

    val builder = client.prepareSearch("baseflow_area").setTypes("baseflow_area")
    //      .setQuery(QueryBuilders.matchQuery("id","5d6c23ad-052b-44bb-ad51-ae984d2cf58b"))
    //        .setQuery(QueryBuilders.matchQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38"))
    //      .get()
    //      .setQuery(QueryBuilders.matchQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38")).get()

//        val builder1 = QueryBuilders.matchPhraseQuery("uuid","dcc3c7bc90c4b33fb11ddd3034a7a3b9")
//        val builder2 = QueryBuilders.matchPhraseQuery("uuid","d6ff271625ffec8bd1a3753a3fcf5c38")
//    val builder2 = QueryBuilders.matchPhraseQuery("article_id", "001")

    val sr = builder.setQuery(QueryBuilders.boolQuery()
//      .must(builder1)
//      .must(builder2)
    )
      .execute()
      .actionGet()
    val hits: Array[SearchHit] = sr.getHits.getHits

    println(hits.size)

    //es中没有该记录
    if(hits.length==0){
      println("写到kafka")
    }


    for (r <- hits) {
      println(r.getSourceAsString)
      println(r.getId)

    }
    client.close()
  }

}
